package com.sinaapp.kjWeb.action;

import java.io.IOException;
import java.io.InputStream;

import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.impl.client.CloseableHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mysql.jdbc.StringUtils;
import com.sinaapp.kjWeb.Exception.WrongStatusException;
import com.sinaapp.kjWeb.util.ConfigUtils;
import com.sinaapp.kjWeb.util.StringUtil;



/*
 *auth:徐疆
 *date 2014-1-12 下午4:13:10
 */
public abstract class Action  {
	public static Logger logger=LoggerFactory.getLogger(Action.class);
	private Action nextAction;
	private String actionName;
	private CloseableHttpClient httpClient;
	public static int maxTryNumber;
	public static int sleepSeconds;
	static{
		String smtn =ConfigUtils.getInstance().getValueAndDefault("kj_max_try_number","1");
		maxTryNumber=Integer.parseInt(smtn);
		sleepSeconds=Integer.parseInt(ConfigUtils.getInstance().getValueAndDefault("kj_nextTry_sleep_seconds","30"));
	}
	
	public Action() {
		super();
	}

	public Action(String actionName) {
		super();
		this.actionName = actionName;
	}

	public Action(String actionName, CloseableHttpClient httpClient) {
		super();
		this.actionName = actionName;
		this.httpClient = httpClient;
	}

	public void mainStart() throws InterruptedException{
		mainStart(maxTryNumber,1);
	}
	
	public void mainStart(int maxTry,int ct) throws InterruptedException{
		if(ct>maxTry){
			throw new RuntimeException("在"+getActionName()+"尝试最"+ct+"次失败,请人工干预操作");
		}
		try{
			process();
		}catch(Exception e){
			logger.error("系统休眠2分钟后再次尝试第"+ct+"次进行"+getActionName(),e);
			Thread.sleep(1000*sleepSeconds);//休眠sleepSeconds后再次尝试
			mainStart(maxTry,ct++);
		}
	}
	
	private void process(){
		doAction();
		if(nextAction!=null){
			if(nextAction.getHttpClient()==null){
				nextAction.setHttpClient(httpClient);
			}
			nextAction.process();
		}
	}
	protected  abstract void doAction();
	public Action getNextAction() {
		return nextAction;
	}
	public void setNextAction(Action nextAction) {
		this.nextAction = nextAction;
	}
	protected void checkStatus(HttpResponse response){
		int status=response.getStatusLine().getStatusCode();
		if(status!=200){
			throw new WrongStatusException(status);
		}
	}
	protected String getStrFromStream(InputStream in){
		try {
			return IOUtils.toString(in,"utf-8");
		} catch (IOException e) {
			logger.error("", e);
			e.printStackTrace();
		}
		return "";
	}
	protected JSONObject getJsonResult(InputStream in){
		String result=getStrFromStream(in);
		if(JSONUtils.mayBeJSON(result)){
			logger.info(getActionName()+"开始解析返回值原始返回信息:"+result);
			JSONObject j=JSONObject.fromObject(result);
			return j;
		}else{
			logger.error(getActionName()+"访问时返回信息不是所期望的:"+result);
		}
		return null;
	}

	public String getActionName() {
		return actionName;
	}

	public void setActionName(String actionName) {
		this.actionName = actionName;
	}

	public CloseableHttpClient getHttpClient() {
		return httpClient;
	}

	public void setHttpClient(CloseableHttpClient httpClient) {
		this.httpClient = httpClient;
	}
	
	
}

